package com.demo.apparel.controller.web;

import com.demo.apparel.common.Result;
import com.demo.apparel.entity.Menu;
import com.demo.apparel.service.web.WebMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/web")
@Tag(name = "菜单接口", description = "菜单相关接口")
public class WebMenuController {

    @Autowired
    private WebMenuService webMenuService;

    @GetMapping("/menu")
    @Operation(summary = "获取菜单树")
    public Result<List<Menu>> getMenuTree() {
        List<Menu> menuList = webMenuService.getMenuTree();
        // 这里直接返回Result封装，前端格式就是你需要的了
        return Result.success(menuList);
    }

    // 新增菜单
    @PostMapping("/add")
    public Result<?> addMenu(@RequestBody Menu menu) {
        boolean success = webMenuService.addMenu(menu);
        return success ? Result.success("新增菜单成功") : Result.fail("新增菜单失败");
    }

    // 删除菜单
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteMenu(@PathVariable Integer id) {
        boolean success = webMenuService.deleteMenu(id);
        return success ? Result.success("删除菜单成功") : Result.fail("删除菜单失败");
    }

    // 编辑菜单
    @PutMapping("/edit")
    public Result<?> editMenu(@RequestBody Menu menu) {
        boolean success = webMenuService.updateMenu(menu);
        return success ? Result.success("编辑菜单成功") : Result.fail("编辑菜单失败");
    }
}
